<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跟随圈</title>
    <style>
        body{
            margin: 0;
            height: 100vh;
            overflow: hidden;
        }

        .box{
            width: 20px;
            height: 20px;
            background-color: black;
            border-radius: 50%;

            position: absolute;
            top: 50%;
            left: 50%;

            transform: translate(-50%, -50%);

        }

        .box:hover{
            background-color: aqua;
        }
    </style>
</head>
<body>
    <div class="box">
    </div>

    <!-- <div class="abc">
        <span class="x"></span><span class="y"></span>
    </div> -->
    

    <script>
        const box = document.querySelector('.box');

        let mouseX = window.innerWidth / 2;
        let mouseY = window.innerHeight / 2;

        let currentX = window.innerWidth / 2;
        let currentY = window.innerHeight / 2;

        box.style.left = `${currentX}px`;
        box.style.top = `${currentY}px`;
        
        const easeFactor = 0.01; // 缓动系数，值越小越慢

        // 监听鼠标移动事件
        document.addEventListener('mousemove', function(e) {
            // 获取鼠标位置
            mouseX = e.clientX;
            mouseY = e.clientY;
        });

        function animate() {
            // 计算当前位置向目标位置靠近一点
            currentX += (mouseX - currentX) * easeFactor;
            currentY += (mouseY - currentY) * easeFactor;
            // 设置圆心对齐鼠标位置
            box.style.left = `${currentX}px`;
            box.style.top = `${currentY}px`;

            requestAnimationFrame(animate);
        }

        animate();
    </script>
</body>
</html>